<template>
<el-drawer :title="!dataForm.id ? '新建' : '编辑'" :visible.sync="visible"
           :wrapperClosable="false" size="40%" append-to-body
           class="JNPF-common-drawer">
<div class="JNPF-flex-main">
<div class="dynamicForm ">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="122px" label-position="right" >
    <template v-if="!loading">
            <el-col :span="12"  >
                <el-form-item  label="资产名称"
 prop="assetName" >
                    <popupTableSelect   v-model="dataForm.assetName"
 placeholder="请选择"  clearable  filterable                         field="assetName" interfaceId="612769091110831365" :columnOptions="assetNamecolumnOptions" propsValue="id" relationField="zcname" popupType="popover"
 popupTitle="选择数据"  popupWidth="800px"
 hasPage :pageSize="20"   >

                </popupTableSelect>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资日期"
 prop="financingStartDate" >
                    <el-date-picker   v-model="dataForm.financingStartDate"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资终止日期"
 prop="financingEndDate" >
                    <el-date-picker   v-model="dataForm.financingEndDate"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资总金额"
 prop="financingAmount" >
                    <el-input-number   v-model="dataForm.financingAmount"
 placeholder="数字文本"  :step="1" >

                </el-input-number>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资合同"
 prop="financingContract" >
                    <el-input   v-model="dataForm.financingContract"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资用途"
 prop="financingPurpose" >
                    <el-input   v-model="dataForm.financingPurpose"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="债权人"
 prop="creditor" >
                    <el-input   v-model="dataForm.creditor"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="债务人"
 prop="debtor" >
                    <el-input   v-model="dataForm.debtor"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="备注"
 prop="notes" >
                    <el-input   v-model="dataForm.notes"
 placeholder="请输入"  :style='{"width":"100%"}' true  type="textarea"  :autosize='{"minRows":4,"maxRows":4}' >

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12"  >
                <el-form-item  label="融资附件"
 prop="financingFile" >
                    <JNPF-UploadFz   v-model="dataForm.financingFile"
 :fileSize="10"    sizeUnit="MB"  :limit="9"  buttonText="点击上传" >

                </JNPF-UploadFz>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </div>
        <div class="drawer-footer">
            <el-button @click="visible = false"> 取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
        </div>
    </div>
    </el-drawer>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
            },
                tableRows:{
                },
            currVmodel:"",
            dataForm: {
                                assetName : "",
                                    financingStartDate : '',
                                    financingEndDate : '',
                                    financingAmount : undefined,
                                    financingContract : '',
                                    financingPurpose : '',
                                    creditor : '',
                                    debtor : '',
                                    notes : '',
                                financingFile : [],
            },
            rules:
            {
                            assetName: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            financingStartDate: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            financingEndDate: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            financingAmount: [
                                {
                                    required: true,
                                    message: '数字文本',
                                    trigger: ["blur","change"]
                                },
                        ],



            },
              assetNamecolumnOptions:[{
                value: 'zcname',
                label: '资产名称'
              }

                ,
                {
                  value: 'zcnumber',
                  label: '资产编码'
                },

                {
                  value: 'address',
                  label: '地址'
                }],

        }
        },
        computed: {},
        watch: {},
        created() {
        },
        mounted() {},
        methods: {
            clearData(){
                               this.dataForm.assetName = "";
                               this.dataForm.financingStartDate = '';
                               this.dataForm.financingEndDate = '';
                                this.dataForm.financingAmount = undefined,
                               this.dataForm.financingContract = '';
                               this.dataForm.financingPurpose = '';
                               this.dataForm.creditor = '';
                               this.dataForm.debtor = '';
                               this.dataForm.notes = '';
                                 this.dataForm.financingFile = [];
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/ZcrzZcrz/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/ZcrzZcrz',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/ZcrzZcrz/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
        _data.financingFile = Array.isArray(_data.financingFile)? JSON.stringify(_data.financingFile):'[]'
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
        _dataAll.financingFile = _dataAll.financingFile ?JSON.parse(_dataAll.financingFile):[]
                this.dataForm = _dataAll
            },
        },
    }

</script>
